/***
This do-file retrieves numbers we use in the paper.
***/

*-------------------------------------------------------------------------------
* Set up 
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

local category "Employment"
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

cap erase "${root}/results/paper numbers/`category'/Self Employment and Population.yaml"

*-------------------------------------------------------------------------------
**# 1. Growth in working age population from Jan 2020 to Jan 2022
*-------------------------------------------------------------------------------

* From https://fred.stlouisfed.org/series/LFWA64TTUSM647S (Working Age Population: Aged 15-64: All Persons for the United States)
project, uses("${root}/data/dvc/St Louis Fed/working_age_pop_LFWA64TTUSM647S.csv")
import delimited "${root}/data/dvc/St Louis Fed/working_age_pop_LFWA64TTUSM647S.csv", clear
gisid date 

rename lfwa64ttusm647s working_age_pop
replace working_age_pop = working_age_pop / 1E6    // in millions 

foreach year in 2020 2022 {
	sum working_age_pop if date == "`year'-01-01"
	local working_age_pop_`year': di %4.1f r(mean)
	
	yamlout using "${root}/results/paper numbers/`category'/Self Employment and Population.yaml",  ///
		key("working_age_pop_`year'") ///
		comment("Working Age Population in Jan `year', Millions") ///
		value(`working_age_pop_`year'') fmt(%4.1f)	
}

*-------------------------------------------------------------------------------
**# 2. Self-Employed Share of Individuals Aged 16 or Older from Jan 2020 to Dec 2021 
*-------------------------------------------------------------------------------

* From https://fred.stlouisfed.org/series/CNP16OV (Civilian Noninstitutional Population 16+, Thousands)
project, uses("${root}/data/dvc/St Louis Fed/pop_16plus_CNP16OV.csv")
import delimited "${root}/data/dvc/St Louis Fed/pop_16plus_CNP16OV.csv", clear 
gisid date

rename cnp16ov pop 
keep if inlist(date, "2020-01-01", "2021-12-01")

tempfile pop 
save `pop'

* From https://fred.stlouisfed.org/series/LNU02048984 (Incorporated Self-Employed, Thousands)
project, uses("${root}/data/dvc/St Louis Fed/incorporated_self_employed_LNU02048984.csv")
import delimited "${root}/data/dvc/St Louis Fed/incorporated_self_employed_LNU02048984.csv", clear 
gisid date

rename lnu02048984 incorporated_self_employed
keep if inlist(date, "2020-01-01", "2021-12-01")

tempfile incorporated  
save `incorporated'

* From https://fred.stlouisfed.org/series/LNU02027714 (Unincorporated Self-Employed, Thousands)
project, uses("${root}/data/dvc/St Louis Fed/unincorporated_self_employed_LNU02027714.csv")
import delimited "${root}/data/dvc/St Louis Fed/unincorporated_self_employed_LNU02027714.csv", clear 
gisid date

rename lnu02027714 unincorporated_self_employed
keep if inlist(date, "2020-01-01", "2021-12-01")

* Merge 
merge 1:1 date using `pop', assert(3) nogen 
merge 1:1 date using `incorporated', assert(3) nogen 

* Share self-employment (%)
gen pct_self_employed = (incorporated_self_employed + unincorporated_self_employed) / pop * 100

* Scalars 
sum pct_self_employed if date == "2020-01-01"
local pct_self_employed_jan2020: di %3.2f r(mean)

sum pct_self_employed if date == "2021-12-01"
local pct_self_employed_dec2021: di %3.2f r(mean)

yamlout using "${root}/results/paper numbers/`category'/Self Employment and Population.yaml",  ///
	key("pct_self_employed_jan2020") ///
	comment("Percent Self Employed, Jan 2020") ///
	value(`pct_self_employed_jan2020') fmt(%3.2f)
	
yamlout using "${root}/results/paper numbers/`category'/Self Employment and Population.yaml",  ///
	key("pct_self_employed_dec2021") ///
	comment("Percent Self-Employed, Dec 2021") ///
	value(`pct_self_employed_dec2021') fmt(%3.2f)

*-------------------------------------------------------------------------------
* Output numbers for paper 
*-------------------------------------------------------------------------------

project, creates("${root}/results/paper numbers/`category'/Self Employment and Population.yaml")
